package com.bwie.config;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.bwie.mapper.ReportMapper;
import com.bwie.pojo.ReportNation;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author GaoYiJie
 * @Description
 * @Create 2025/4/17 11:54
 * @Version 1.0
 */
@Configuration
public class InitReportToRedis {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    ReportMapper reportMapper;

    @PostConstruct
    public void initReportToRedis(){
        List<ReportNation> reportNations = reportMapper.selectList(null);

        //生成随机数 :  预防缓存雪崩
        int randomInt = RandomUtil.randomInt(0, 10);

        String reportNationsKey = "report_list";
        stringRedisTemplate.opsForValue().set(reportNationsKey, JSON.toJSONString(reportNations),30+randomInt, TimeUnit.MINUTES);
    }
}
